﻿Imports System.Data.SqlClient
Public Class frm_dangkyhoc
    Sub loadhocvien()
        Dim cmd As New SqlCommand("sp_hocvien_select", conn)
        cmd.CommandType = CommandType.StoredProcedure

        Dim da As New SqlDataAdapter(cmd)
        Dim tb As New DataTable
        da.Fill(tb)

        dgv_dangkhoc.DataSource = tb
    End Sub
    Private Sub frm_dangkyhoc_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadhocvien()
    End Sub

    Private Sub cmd_dangky_Click(sender As Object, e As EventArgs) Handles cmd_dangky.Click
        If cmd_dangky.Text = "Đăng Ký" Then
            Dim cmd As New SqlCommand("sp_hocvien_dangky", conn)
            cmd.CommandType = CommandType.StoredProcedure

            cmd.Parameters.AddWithValue("@mahv", txt_maHV.Text)
            cmd.Parameters.AddWithValue("@tehocvien", txt_tenHV.Text)
            cmd.Parameters.AddWithValue("@gioitinh", cbo_GT.Text)
            cmd.Parameters.AddWithValue("@ngaysinh", dtp_ngaysinh.Value)
            cmd.Parameters.AddWithValue("@diachi", txt_diachi.Text)
            cmd.Parameters.AddWithValue("@soDT", txt_sodienthoai.Text)

            Dim i As Integer = cmd.ExecuteNonQuery
            If i > 0 Then
                MsgBox("Đăng ký  học thành công")
                loadhocvien()
                cmd_huy_Click(sender, e)
            Else
                MsgBox("Bạn chưa đăng ký thành công")
            End If
        End If
        If cmd_dangky.Text = "Sửa" Then
            Dim cmd As New SqlCommand("sp_hocvien_update", conn)
            cmd.CommandType = CommandType.StoredProcedure

            cmd.Parameters.AddWithValue("@mahocvien", txt_maHV.Text)
            cmd.Parameters.AddWithValue("@tenhocvien", txt_tenHV.Text)
            cmd.Parameters.AddWithValue("@gioitinh", cbo_GT.Text)
            cmd.Parameters.AddWithValue("@ngaysinh", dtp_ngaysinh.Value)
            cmd.Parameters.AddWithValue("@diachi", txt_diachi.Text)
            cmd.Parameters.AddWithValue("@sodienthoai", txt_sodienthoai.Text)

            Dim i As Integer = cmd.ExecuteNonQuery
            If i <= 0 Then
                MsgBox("Thông tin sinh viên chưa được sửa")
            Else
                MsgBox("Sửa thông tin sinh viên thành công")
                loadhocvien()
                cmd_huy_Click(sender, e)
            End If
        End If
    End Sub
    Private Sub dgv_dangkhoc_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv_dangkhoc.DoubleClick
        Dim r As DataGridViewRow = dgv_dangkhoc.CurrentRow
        txt_maHV.Text = r.Cells("MaHocVien").Value
        txt_tenHV.Text = r.Cells("TenHocVien").Value
        cbo_GT.Text = r.Cells("GioiTinh").Value
        dtp_ngaysinh.Value = r.Cells("NgaySinh").Value
        txt_diachi.Text = r.Cells("DiaChi").Value
        txt_sodienthoai.Text = r.Cells("SoDT").Value
        cmd_dangky.Text = "Sửa"
    End Sub

    Private Sub cmd_huy_Click(sender As Object, e As EventArgs) Handles cmd_huy.Click
        For Each k As Control In Me.Controls
            If TypeOf k Is TextBox Then
                k.Text = ""
            End If
        Next
    End Sub
End Class